package main

import (
	"algorithms/stack"
	"fmt"
)

/*leetcode 20题 括号匹配*/

// 使用go 切片
func isValid0(s string) bool {
	var stack []rune
	for _, c := range s {
		if c == '(' || c == '[' || c == '{' {
			stack = append(stack, c)
		} else {
			var e rune
			if len(stack) != 0 {
				e = stack[len(stack)-1]
				stack = stack[:len(stack)-1]
			}
			if c == ')' && e != '(' {
				return false
			}
			if c == ']' && e != '[' {
				return false
			}
			if c == '}' && e != '{' {
				return false
			}
		}
	}
	if len(stack) == 0 {
		return true
	} else {
		return false
	}
}

//使用自定义stack
func isValid(s string) bool {
	stack := stack.MakeArrayStack[rune]()
	for _, c := range s {
		if c == '(' || c == '[' || c == '{' {
			stack.Push(c)
		} else {
			var e rune
			if !stack.IsEmpty() {
				e, _ = stack.Pop()
			}
			if c == ')' && e != '(' {
				return false
			}
			if c == ']' && e != '[' {
				return false
			}
			if c == '}' && e != '{' {
				return false
			}
		}
	}
	if stack.IsEmpty() {
		return true
	} else {
		return false
	}
}

func main() {
	fmt.Println(isValid0("0"))
	fmt.Println(isValid0("[](){}"))
	fmt.Println(isValid0("([)]"))

	fmt.Println(isValid("0"))
	fmt.Println(isValid("[](){}"))
	fmt.Println(isValid("([)]"))
}
